package com.lzx.shop.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lzx.shop.dataobject.OrderDO;
import com.lzx.shop.model.Paging;
import com.lzx.shop.model.Result;
import com.lzx.shop.service.OrderService;
import com.lzx.shop.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName OrderApi.java
 * @Description TODO
 * @createTime 2021年03月22日 23:41:00
 */
@Api(tags = "OrderApi",description = "订单管理")
@RestController
@RequestMapping("/order")
public class OrderApi {

    @Autowired
    private OrderService orderService;

    @ApiOperation("查询所有订单")
    @GetMapping("/list")
    public Result list(){
        return ResultUtil.success(orderService.findAll());
    }

    @ApiOperation("插入订单")
    @PostMapping("/save")
    public Result save(@RequestBody OrderDO object){
        if (object==null){
            return ResultUtil.paramError();
        }

        return ResultUtil.success(orderService.save(object));
    }

    @ApiOperation("更新订单")
    @PostMapping("/update")
    public Result update(@RequestBody OrderDO object){
        if (object == null){
            return ResultUtil.paramError();
        }
        return  ResultUtil.success(orderService.update(object));
    }

    @ApiOperation("删除订单")
    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable("id") String id){
        if (StringUtils.isBlank(id)){
            return ResultUtil.paramError();
        }
        return  ResultUtil.success(orderService.DeleteById(id));
    }

    @ApiOperation("根据用户id查询")
    @GetMapping("/getByUser/{id}")
    public Result getByUserId(@PathVariable("id")String id){
        if (StringUtils.isBlank(id)){
            return  ResultUtil.paramError();
        }
        List<OrderDO> byUserId = orderService.findByUserId(id);
        return ResultUtil.success(byUserId);
    }

    @ApiOperation("关联查询")
    @GetMapping("/getAllNeed")
    public Result getAllNeed(@RequestParam("page")Integer page,@RequestParam("limit")Integer limit){

        Page<OrderDO> objects = PageHelper.startPage(page, limit).doSelectPage(() -> orderService.findAllNeed());
        Paging<OrderDO> rPaging = new Paging<>(objects.getPageNum(), objects.getPageSize(), objects.getPages(), objects.getTotal(), objects
                .getResult());
//        List<OrderDO> byUserId = orderService.findAllNeed();
        return ResultUtil.success(rPaging);
    }
}
